#ifndef ACUAS_H
#define ACUAS_H
/** @mainpage ACUAS mCEasy Version 2012
*
* @section sec1 Introduction

<img src="ACUAS-mCEasy.jpg" alt="ACUAS mC Board">

For beginners the programming of microcontrollers is hard. Even if they have already programming experience in programming PCs the handling of the microcontroller specific hardware can be a challenge.
Not only the 351 pages of the ATmega16 data sheet must be understood but a lot of experience in assembler programming is required. Even then it can be a tedious and frustrating work.
The goal of our approach is to provide a hardware and software environment for beginners  in the domain of microcontrollers who want to explore the potentials. In addition the flexible concept provides the full advantage of C for experienced microcontroller programmers.

The mCEasy programming interface provides a set of instructions which program the microcontroller on the ACUAS board. Programming and testing is done a PC. During build time the instructions are preprocessed to C programming language and then compiled and linked. The result is a .hex file which can be transferred the controller. For uploading the hex file to the microcontroller see appendix.

Technically the instructions are C macros by which the concept is fully integrated with C. Hence all C programming features can optionally be used and mixed with the mCEasy Programming instructions. This assures full flexibility as the student wants to program more sophisticated algorithms.

 @author Thomas Siepmann, Aachen University of Applied Sciences, Germany, 2012
 @see http://www.siepmann.fh-aachen.de
*/


/**
 @defgroup ACUAS_EP ACUAS Easy Programming

 @brief C macros to easily program the ACUAS ATmega16/ATmega32 board.
 @author Thomas Siepmann, Aachen University of Applied Sciences, Germany
 @see http://www.siepmann.fh-aachen.de
 */

/// @{
/// @file ACUAS.h
/// @file ACUAS.c

#ifdef BUTTERFLY
#include "ACUAS-Butterfly.h"
#endif

#ifndef F_CPU
/// default processor speed (used for WAIT_SEC and UART)
#define F_CPU 100000UL
#endif

#include <avr/io.h>
#include <stdlib.h>

void set_seed(void);
void InitBoard(void);
void delay_ds(unsigned char ds);
void autoADCps(void);
char lcd_str[10];


/**
 *  @name  Program main structure
 Set simulation modus:
 No waiting (WAIT_SEC) and constant random seed (for testing). Must be the first line in .c file:
 define \#SIMULATION
 */
//   Begin of instructions which are carried out only one time at the beginning of the program
#define PROGRAM_INIT   int main(void){InitBoard();
//   Begin of instructions which are carried out in an endless loop.
#define PROGRAM_START  while(1){
//   End of the program
#define PROGRAM_END    } return 0;}


//Define Variables

//   Declares a 32 bit integer variable with a range from - 2147483648 to + 2147483648
#define VARIABLE(v) signed long int v;
//   Declares a 8 bit unsigned variable with a range from 0 to 255
#define VAR(v) unsigned char v;
//   Declares a 16 bit unsigned variable with a range from 0 to 65535
#define VAR16(v) uint16_t v;
//   Declares a character array s which can contain a string of max characters
#define STRING(s,max) unsigned char s[(max)+1];
//   Declares a CONSTANT character array s which contains the string string
#define STRING_CONST(s,string) static const char s[] PROGMEM = string;


//Miscellaneous

//   Waits x seconds. Range of x i from 0.1 to 25
#define WAIT_SEC(x) delay_ds(x*10);
//   Empty instruction. Continues immediately with the next instruction. Can be used for empty loops or before else conditions. Consumes no time.
#define DO_NOTHING ;

//   Sets the bit number b to 1. Ex.: SET_BIT(PORTC, 7)
#define SET_BIT(r,b) (r) |= 1 << (b);
//   Sets the bit number b to 0. Ex.: CLEAR_BIT(PORTC, 7)
#define CLEAR_BIT(r,b) (r) &= ~(1 << (b));

//Joystick Center

//   Execute the following instruction if the joystick up button is pressed
#define ON_JOYSTICK_CENTER     if(!(PINB & (1 << 4)))
//   Execute the following instruction if the joystick up button is not pressed
#define OFF_JOYSTICK_CENTER      if(PINB & (1 << 4))
//   Wait until the joystick up button is pressed
#define WAIT_FOR_JOYSTICK_CENTER while(PINB & (1 << 4));
//   Wait until the joystick up button is released
#define WAIT_FOR_RELEASE_JOYSTICK_CENTER while(!(PINB & (1 << 4)));

//Joystick up

//   Execute the following instruction if the joystick up button is pressed
#define ON_JOYSTICK_UP     if(!(PINB & (1 << 6)))
//   Execute the following instruction if the joystick up button is not pressed
#define OFF_JOYSTICK_UP      if(PINB & (1 << 6))
//   Wait until the joystick up button is pressed
#define WAIT_FOR_JOYSTICK_UP while(PINB & (1 << 6));
//   Wait until the joystick up button is released
#define WAIT_FOR_RELEASE_JOYSTICK_UP while(!(PINB & (1 << 6)));

//Joystick down

//   Execute the following instruction if the joystick down button is pressed
#define ON_JOYSTICK_DOWN   if(!(PINB & (1 << 7)))
//   Execute the following instruction if the joystick down button is not pressed
#define OFF_JOYSTICK_DOWN    if(PINB & (1 << 7))
//   Wait until the joystick down button is pressed
#define WAIT_FOR_JOYSTICK_DOWN while(PINB & (1 << 7));
//   Wait until the joystick down button is released
#define WAIT_FOR_RELEASE_JOYSTICK_DOWN while(!(PINB & (1 << 7)));

//Joystick right

//   Execute the following instruction if the joystick right button is pressed
#define ON_JOYSTICK_RIGHT  if(!(PINE & (1 << 2)))
//   Execute the following instruction if the joystick right button is not pressed
#define OFF_JOYSTICK_RIGHT   if(PINE & (1 << 2))
//   Wait until the joystick right button is pressed
#define WAIT_FOR_JOYSTICK_RIGHT while(PINE & (1 << 2));
//   Wait until the joystick right button is released
#define WAIT_FOR_RELEASE_JOYSTICK_RIGHT while(!(PINE & (1 << 2)));

//Joystick left

//   Execute the following instruction if the joystick left button is pressed
#define ON_JOYSTICK_LEFT   if(!(PINE & (1 << 3)))
//   Execute the following instruction if the joystick left button is not pressed
#define OFF_JOYSTICK_LEFT    if(PINE & (1 << 3))
//   Wait until the joystick left button is pressed
#define WAIT_FOR_JOYSTICK_LEFT while(PINE & (1 << 3));
//   Wait until the joystick left button is released
#define WAIT_FOR_RELEASE_JOYSTICK_LEFT while(!(PINE & (1 << 3)));


//ADC

//Activate ADC with Prescaler
#define ACTIVATE_ADC   ADCSRA = (1<<ADEN) | (1<<ADPS1) | (1<<ADPS0); 
//Select channel. Channel numbering is 1 based.
#define ADC_CHANNEL(ch) ADMUX = ch-1;
//Perform a 10 bit AD conversion and store the digital value in a 16 bit variable.
#define ADCONVERT(x) x=ADC_read(); 
//Perform a 10 bit AD conversion and store the value in a 8 bit variable.
#define ADCONVERTlow(vc) {vc=ADC_read(); vc=vc >> 2; };
//Perform a 10 bit AD conversion and store the result in mV in an integer (or double or float) variable.
#define ADCONVERT_MV(x) {x=ADC_read(); x=x*3./1.024; };


//SENSORS

#define TEMPERATURE  {ADC_init(0); ADC_periphery();};

#define VOLTAGE  {ADC_init(1); ADC_periphery();};

#define BRIGHTNESS  {ADC_init(2); ADC_periphery();};

//BEEP

#define BEEP_VOLUME(vol) Volumen(vol);
#define BEEP(tone,duration) {Sound_Init(); Play_Tone(tone,duration);};

//LCD 

//   Initializes the LCD and clears the screen.
#define ACTIVATE_LCD {LCD_Init();sei();};
//   Clear LCD screen.
#define CLEAR_LCD LCD_Clear(); 
//   Write string at cursor position.
#define LCD_TEXT(string) LCD_puts(string, 0);
//   Write integer number at cursor position.
#define LCD_NUMBER(v) {itoa( (v),lcd_str,10); LCD_puts(lcd_str,0);};
//   Write double precision number at cursor position.Conversion is done in the format "[-]d.ddd".
#define LCD_D_NUMBER(v, w, p) {dtostrf((v),w,p,lcd_str); LCD_puts(lcd_str,0);};
//   Write 1 character (char) at cursor position.
#define LCD_CHAR(c) LCD_putc(0,c);


//  Flash Memory
#define WRITE_FLASH(nb,datab) Write_Flash_Memory (nb, datab);
#define READ_FLASH(n,data) Read_Flash_Memory (n, data);
#define ERASE_FLASH() Erase_Page ();





// FUNCTIONS
// #include "ACUAS.c"

/*@}*/
#endif // ACUAS_H
